package com.ibigcat.module.mdm.controller.admin.coderule;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.CommonResult;
import com.ibigcat.framework.common.util.object.BeanUtils;
import static com.ibigcat.framework.common.pojo.CommonResult.success;

import com.ibigcat.framework.excel.core.util.ExcelUtils;

import com.ibigcat.framework.apilog.core.annotation.ApiAccessLog;
import static com.ibigcat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ibigcat.module.mdm.controller.admin.coderule.vo.*;
import com.ibigcat.module.mdm.dao.entity.CodeRuleDO;
import com.ibigcat.module.mdm.service.CodeRuleService;

@Tag(name = "管理后台 - 编码规则配置")
@RestController
@RequestMapping("/mdm/code-rule")
@Validated
public class CodeRuleController {

    @Resource
    private CodeRuleService codeRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建编码规则配置")
    @PreAuthorize("@ss.hasPermission('mdm:code-rule:create')")
    public CommonResult<Long> createCodeRule(@Valid @RequestBody CodeRuleSaveReqVO createReqVO) {
        return success(codeRuleService.createCodeRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新编码规则配置")
    @PreAuthorize("@ss.hasPermission('mdm:code-rule:update')")
    public CommonResult<Boolean> updateCodeRule(@Valid @RequestBody CodeRuleSaveReqVO updateReqVO) {
        codeRuleService.updateCodeRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除编码规则配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdm:code-rule:delete')")
    public CommonResult<Boolean> deleteCodeRule(@RequestParam("id") Long id) {
        codeRuleService.deleteCodeRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得编码规则配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdm:code-rule:query')")
    public CommonResult<CodeRuleRespVO> getCodeRule(@RequestParam("id") Long id) {
        CodeRuleDO codeRule = codeRuleService.getCodeRule(id);
        return success(BeanUtils.toBean(codeRule, CodeRuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得编码规则配置分页")
    @PreAuthorize("@ss.hasPermission('mdm:code-rule:query')")
    public CommonResult<PageResult<CodeRuleRespVO>> getCodeRulePage(@Valid CodeRulePageReqVO pageReqVO) {
        PageResult<CodeRuleDO> pageResult = codeRuleService.getCodeRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CodeRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出编码规则配置 Excel")
    @PreAuthorize("@ss.hasPermission('mdm:code-rule:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCodeRuleExcel(@Valid CodeRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CodeRuleDO> list = codeRuleService.getCodeRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "编码规则配置.xls", "数据", CodeRuleRespVO.class,
                        BeanUtils.toBean(list, CodeRuleRespVO.class));
    }

}